﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Common.Models;
using Module.Core.BusinessLogic;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.LoginSystem.Commons.Framwork;

namespace Web.Cms.Member
{
    public partial class LstMember : AdminUserControl
    {
        #region Khai báo biến
        private int totalRecords;
        private int cPage = 1;
        private int page;
        private string email;
        private DateTime dateTo;
        private DateTime dateFrom;
        #endregion

        #region OnInit
        protected override void OnInit(EventArgs e)
        {
            try
            {
                base.OnInit(e);

                if (Request.QueryString["email"] != null || Request.QueryString["email"] != "")
                {
                    txtKeysearch.Text = Request.QueryString["email"];
                }
                if (Request.QueryString["checkType"] != null || Request.QueryString["checkType"] != "")
                {
                    dropType.SelectedValue = Request.QueryString["checkType"];
                }
               if (Request.QueryString["dateFrom"] != null && Request.QueryString["dateFrom"] != "")
                {
                    dateFrom = Convert.ToDateTime(Request.QueryString["dateFrom"].Trim());
                    txtDatefrom.Text = dateFrom.ToString("MM/dd/yyyy");
                }
                else
                {
                    dateFrom = DateTime.Now.AddYears(-2);
                    txtDatefrom.Text = dateFrom.ToString("MM/dd/yyyy");
                }

                if (Request.QueryString["dateTo"] != null && Request.QueryString["dateTo"] != "")
                {
                    dateTo = Convert.ToDateTime(Request.QueryString["dateTo"].Trim());
                    txtDateTo.Text = dateTo.ToString("MM/dd/yyyy");
                }
                else
                {
                    dateTo = DateTime.Now.AddYears(+1);
                    txtDateTo.Text = dateTo.ToString("MM/dd/yyyy");
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region OnPreRender
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                page = int.Parse("0" + Request.QueryString["page"]);
                if (page == 0) page = 1;
                base.OnPreRender(e);
                LoadData();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        #endregion



        #region LoadData
        private void LoadData()
        {
            if (txtKeysearch.Text != "")
                email = txtKeysearch.Text.Trim();
            if (Request.QueryString.Get("cpage") != null)
                cPage = ObjConvertUtility.ToInt32(Request.QueryString.Get("cPage"));
            if (cPage == 0) cPage = 1;
            int from = (cPage - 1) * Const.pageSize30 + 1;
            if (from == 1) from = 0;
            int to = (cPage) * Const.pageSize30;
            totalRecords = MemberBL.GetCount(email,Const.show);
            PagingAjax1.totalRecords = totalRecords;
            PagingAjax1.numPerPage = Const.pageSize30;
            PagingAjax1.queryParam = ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "keyword", "from", "to", "cPage" },
                                    new string[] { email, from.ToString(), to.ToString(), "" });

            PagingAjax1.defaultQueryParam =
                ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "keyword", "from", "to" },
                                    new string[] { email, from.ToString(), to.ToString() });
            CHRTList<MemberInfo> list = MemberBL.SearchAll(email,Const.show, from, to,int.Parse(dropType.SelectedValue), DateTime.Parse(txtDatefrom.Text), DateTime.Parse(txtDateTo.Text));
            if (list != null && list.Count > 0)
            {
                grdMember.Visible = true;
                grdMember.DataSource = list;
                grdMember.RowDataBound += new GridViewRowEventHandler(grdMember_RowDataBound);
                grdMember.DataBind();
                PagingAjax1.Visible = true;
            }
            else
            {
                grdMember.Visible = false;
                lblMessage.Text = "Không tồn tại dữ liệu !";
                PagingAjax1.Visible = false;
            }
        }
        #endregion

        #region RowDataBound



        protected void grdMember_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton btnStatus = (ImageButton)e.Row.FindControl("btnStatus");
                MemberInfo info = (MemberInfo)e.Row.DataItem;

                e.Row.Cells[0].Text = (Const.pageSize30 * (cPage - 1) + e.Row.DataItemIndex + 1).ToString();
              
            }
        }

        #endregion

        #region RowCommand

        protected void grdMember_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "Commanddelete":
                        try
                        {
                            int idMember = Convert.ToInt32(e.CommandArgument);
                            MemberBL.Delete(idMember);
                            LoadData();
                        }
                        catch (Exception ex)
                        {
                            lblMessage.Text = ex.Message;
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        #endregion

        #region tìm kiếm
       
       

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?ctrlkey=" + CurrentCommand.CtrlKey + "&email=" + txtKeysearch.Text.Trim() + "&checkType=" + dropType.SelectedValue + "&dateFrom=" + txtDatefrom.Text.Trim() + "&dateTo=" + txtDateTo.Text.Trim() + "&page=" + page);
        }
        #endregion

    }
}